﻿using BonjourQuotation.DataAccess.Entity_Framework;
using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.BusinessData;
using BonjourQuotation.ObjectModel.ErrorData;
using BonjourQuotation.ObjectModel.PreferredVendorData;
using BonjourQuotation.ObjectModel.LoggerData;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BonjourQuotation.DataAccess.Repositories
{
    public class PreferredVendorRepository : IPreferredVendorRepository
    {
        private readonly BonDataEntities _context = new BonDataEntities();
        private readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public ErrorBInfo CreateVendorContract(PreferredVendorContractsBData VendorContractData)
        {
            ErrorBInfo Error = new ErrorBInfo();
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    int? VendortypID = _context.tblVendors.Where(vnd => vnd.VendorID == VendorContractData.VendorID).Select(vnd => vnd.VendorTypeID).SingleOrDefault();
                    if (VendortypID == 1)
                    {
                        List<int> ServiceIDs = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 79, 87 };
                        int LineNo = 1;
                        foreach (var ServiceID in ServiceIDs)
                        {
                            tblContractDetail NewVendorContractDetail = new tblContractDetail()
                            {
                                LineNo = LineNo,
                                VendorID = VendorContractData.VendorID,
                                SeasonID = VendorContractData.AddContractSelectedSeasonID,
                                Price = 0,
                                StartDate = VendorContractData.ContractStartDate,
                                EndDate = VendorContractData.ContractEndDate,
                                ServiceID = ServiceID,
                                Currency = "USD",
                            };
                            _context.tblContractDetails.Add(NewVendorContractDetail);
                            ++LineNo;
                        }
                    }
                    tblContract NewVendorContract = new tblContract()
                    {
                        VendorID = VendorContractData.VendorID,
                        SeasonID = VendorContractData.AddContractSelectedSeasonID,
                        ContractDate = VendorContractData.ContractDate,
                        ContractStartDate = VendorContractData.ContractStartDate,
                        ContractEndDate = VendorContractData.ContractEndDate,
                        Currency = "USD",
                    };
                    _context.tblContracts.Add(NewVendorContract);
                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    Error.errorCode = 100;
                    Error.Message = ex.Message;
                    logger.Error(ex.ToMessageAndCompleteStacktrace());
                    transaction.Rollback();
                }
            }
            return Error;
        }
        public ErrorBInfo CreateVendorContractDetail(PreferredVendorContractsFromDetailsBData VendorContractDetailData)
        {
            ErrorBInfo Error = new ErrorBInfo();
            int MaxLineNo = 0;
            try
            {
                if (_context.tblContractDetails.Any(CDet => CDet.SeasonID == VendorContractDetailData.ContractTableDetail.SelectedSeasonID
                                                  && CDet.VendorID == VendorContractDetailData.ContractTableDetail.VendorID))
                    MaxLineNo = _context.tblContractDetails.Where(CDet => CDet.SeasonID == VendorContractDetailData.ContractTableDetail.SelectedSeasonID
                                                  && CDet.VendorID == VendorContractDetailData.ContractTableDetail.VendorID).Max(CDet => CDet.LineNo);

                tblContractDetail NewVendorContractDetail = new tblContractDetail()
                {
                    LineNo = MaxLineNo + 1,
                    VendorID = VendorContractDetailData.ContractTableDetail.VendorID,
                    SeasonID = VendorContractDetailData.ContractTableDetail.SelectedSeasonID,
                    Price = VendorContractDetailData.Price,
                    StartDate = VendorContractDetailData.StartDate,
                    EndDate = VendorContractDetailData.EndDate,
                    Description = VendorContractDetailData.Description,
                    ServiceID = VendorContractDetailData.ServiceID,
                    Currency = VendorContractDetailData.CurrencyText,
                };

                _context.tblContractDetails.Add(NewVendorContractDetail);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                Error.errorCode = 100;
                Error.Message = ex.Message;
                logger.Error(ex.ToMessageAndCompleteStacktrace());
            }
            return Error;
        }
        public ManagePreferredVendorsGridBData LoadAllVendors(int PageNumber)
        {
            ManagePreferredVendorsGridBData AllVendors = new ManagePreferredVendorsGridBData();
            AllVendors.Rows = new List<ManagePreferredVendorsGridRowBData>();
            AllVendors.Rows = (from vendor in _context.tblVendors
                               from vtype in _context.tblVendorTypes
                               from area in _context.tblAreas
                               from region in _context.tblRegions
                               where vendor.VendorTypeID == vtype.VendorTypeID
                               && vendor.AreaID == area.AreaID
                               && vendor.RegionID == region.RegionID
                               && vendor.Preferred == true
                               && area.Active == true
                               && region.Active == true
                               && vtype.Active == true
                               orderby vendor.VendorName
                               select new ManagePreferredVendorsGridRowBData()
                                {
                                    VendorID = vendor.VendorID,
                                    VendorName = vendor.VendorName,
                                    SelectedCity = vendor.City,
                                    SelectedState = vendor.StateID,
                                    SelectedVendorTypeCode = vtype.VendorTypeCode,
                                    SelectedRegion = region.RegionDesc,
                                    SelectedArea = area.AreaDesc,
                                }).Take(PageNumber * ManagePreferredVendorsGridBData.PageCount).ToList();

            AllVendors.VendorTypeID = (from vtype in _context.tblVendorTypes
                                       where vtype.Active == true
                                       select new
                                       {
                                           Key = vtype.VendorTypeID,
                                           Value = vtype.VendorTypeCode,
                                       }).ToDictionary(o => o.Key, o => o.Value);
            AllVendors.City = (from city in _context.tblCities
                               where city.Active == true
                               && city.CountryID == 1 || city.CountryID == 7
                               orderby city.City
                               select new
                               {
                                   Key = city.CityID,
                                   Value = city.City
                               }).ToDictionary(o => o.Key, o => o.Value);
            AllVendors.RegionID = (from reg in _context.tblRegions where reg.Active == true orderby reg.RegionDesc select new { Key = reg.RegionID, Value = reg.RegionDesc }).ToDictionary(o => o.Key, o => o.Value);
            AllVendors.StateID = (from state in _context.tblStates where state.Active == true orderby state.StateID select new { Key = state.StateID, Value = state.StateID }).ToDictionary(o => o.Key, o => o.Value);
            AllVendors.AreaID = (from area in _context.tblAreas where area.Active == true orderby area.AreaDesc select new { Key = area.AreaID, Value = area.AreaDesc }).ToDictionary(o => o.Key, o => o.Value);
            return AllVendors;
        }
        public ManagePreferredVendorsGridBData LoadSearchedVendors(ManagePreferredVendorsGridBData Data)
        {
            var FilterVendor = _context.tblVendors.AsQueryable();
            if (Data.SelectedRegionID != null)
                FilterVendor = FilterVendor.Where(ven => ven.RegionID == Data.SelectedRegionID);
            if (Data.SelectedCity != null)
                FilterVendor = FilterVendor.Where(ven => ven.City == Data.SelectedCity);
            if (Data.SelectedVendorTypeID != null)
                FilterVendor = FilterVendor.Where(ven => ven.VendorTypeID == Data.SelectedVendorTypeID);
            if (Data.SelectedAreaID != null)
                FilterVendor = FilterVendor.Where(ven => ven.AreaID == Data.SelectedAreaID);
            if (Data.SelectedStateID != null && Data.SelectedStateID.Length > 0)
                FilterVendor = FilterVendor.Where(ven => ven.StateID == Data.SelectedStateID);
            ManagePreferredVendorsGridBData allvendors;
            allvendors = new ManagePreferredVendorsGridBData();
            allvendors.Rows = new List<ManagePreferredVendorsGridRowBData>();
            allvendors.Rows = (from vendor in FilterVendor
                               from vtype in _context.tblVendorTypes
                               from area in _context.tblAreas
                               from region in _context.tblRegions
                               where vendor.VendorTypeID == vtype.VendorTypeID
                               && vendor.AreaID == area.AreaID
                               && vendor.RegionID == region.RegionID
                               && vendor.Preferred == true
                               && vtype.Active == true
                               && area.Active == true
                               && region.Active == true
                               orderby vendor.VendorName
                               select new ManagePreferredVendorsGridRowBData()
                                {
                                    VendorID = vendor.VendorID,
                                    VendorName = vendor.VendorName,
                                    SelectedCity = vendor.City,
                                    SelectedState = vendor.StateID,
                                    SelectedVendorTypeCode = vtype.VendorTypeCode,
                                    SelectedRegion = area.AreaDesc,
                                    SelectedArea = region.RegionDesc,
                                }).Take(Data.PageNum * ManagePreferredVendorsGridBData.PageCount).ToList();
            allvendors.VendorTypeID = (from vtypes in _context.tblVendorTypes
                                       where vtypes.Active == true
                                       select new
                                       {
                                           Key = vtypes.VendorTypeID,
                                           Value = vtypes.VendorTypeCode
                                       }).ToDictionary(o => o.Key, o => o.Value);
            allvendors.City = (from city in _context.tblCities
                               where city.Active == true
                               && city.CountryID == 1 || city.CountryID == 7
                               orderby city.City
                               select new
                               {
                                   Key = city.CityID,
                                   Value = city.City
                               }).ToDictionary(o => o.Key, o => o.Value);
            allvendors.RegionID = (from reg in _context.tblRegions where reg.Active == true orderby reg.RegionDesc select new { Key = reg.RegionID, Value = reg.RegionDesc }).ToDictionary(o => o.Key, o => o.Value);
            allvendors.StateID = (from state in _context.tblStates where state.Active == true orderby state.StateID select new { Key = state.StateID, Value = state.StateID }).ToDictionary(o => o.Key, o => o.Value);
            allvendors.AreaID = (from area in _context.tblAreas where area.Active == true orderby area.AreaDesc select new { Key = area.AreaID, Value = area.AreaDesc }).ToDictionary(o => o.Key, o => o.Value);
            return allvendors;
        }
        public PreferredVendorDetailsBData LoadVendorDetailsByID(int VendorID)
        {
            PreferredVendorDetailsBData VendorData = null;
            if (VendorID == 0)
            {
                VendorData = new PreferredVendorDetailsBData();
                VendorData.StateID = new Dictionary<string, string>();
                VendorData.StateID = (from state in _context.tblStates where state.Active == true orderby state.StateID select new { Key = state.StateID, Value = state.StateID }).ToDictionary(o => o.Key, o => o.Value);
                VendorData.RegionID = (from reg in _context.tblRegions where reg.Active == true && reg.CountryID == 1 orderby reg.RegionDesc select new { Key = reg.RegionID, Value = reg.RegionDesc }).ToDictionary(o => o.Key, o => o.Value);
                VendorData.City = new Dictionary<int, string>();
                VendorData.AreaID = new Dictionary<int, string>();
                VendorData.SelectedCountryID = 1;
            }
            else
            {
                VendorData = (from Ven in _context.tblVendors
                              where Ven.VendorID == VendorID
                              select new PreferredVendorDetailsBData
                              {
                                  VendorID = Ven.VendorID,
                                  VendorName = Ven.VendorName,
                                  SelectedVendorTypeID = Ven.VendorTypeID,
                                  ContactName = Ven.ContactName,
                                  ContactTitle = Ven.ContactTitle,
                                  Preferred = Ven.Preferred,
                                  Active = Ven.Active,
                                  IsFind = Ven.IsFind,
                                  IsComp = Ven.IsComp,
                                  Address = Ven.Address,
                                  SelectedCity = Ven.City,
                                  SelectedStateID = Ven.StateID,
                                  PostalCode = Ven.PostalCode,
                                  Phone800 = Ven.Phone800,
                                  PhoneNumber = Ven.PhoneNumber,
                                  PhoneSales = Ven.PhoneSales,
                                  FaxNumber = Ven.FaxNumber,
                                  FaxSales = Ven.FaxSales,
                                  SelectedRegionID = Ven.RegionID,
                                  SelectedAreaID = Ven.AreaID,
                                  SelectedCountryID = Ven.CountryID,
                                  Notes = Ven.Notes,
                                  ArrivalDate = Ven.ArrivalDate,
                                  DepartureDate = Ven.DepartureDate,
                                  Web = Ven.Web,
                                  Email = Ven.EmailName,
                                  EmailSales = Ven.EmailNameSales,
                                  CityID = Ven.CityID,
                              }).SingleOrDefault();
                if (VendorData.SelectedCountryID == 1)
                {
                    VendorData.StateID = (from state in _context.tblStates where state.Active == true orderby state.StateID select new { Key = state.StateID, Value = state.StateID }).ToDictionary(o => o.Key, o => o.Value);
                    if (VendorData.SelectedStateID == null)
                    {
                        VendorData.City = new Dictionary<int, string>();
                        VendorData.RegionID = (from reg in _context.tblRegions where reg.Active == true && reg.CountryID == 1 orderby reg.RegionDesc select new { Key = reg.RegionID, Value = reg.RegionDesc }).ToDictionary(o => o.Key, o => o.Value);
                        if (VendorData.RegionID == null)
                        {
                            VendorData.RegionID = new Dictionary<int, string>();
                            VendorData.AreaID = new Dictionary<int, string>();
                        }
                        else if (VendorData.RegionID.Any(reg => reg.Key == VendorData.SelectedRegionID))
                        {
                            VendorData.AreaID = (from area in _context.tblAreas where area.RegionID == VendorData.SelectedRegionID && area.Active == true orderby area.AreaDesc select new { Key = area.AreaID, Value = area.AreaDesc }).ToDictionary(o => o.Key, o => o.Value);
                        }
                        else
                            VendorData.AreaID = new Dictionary<int, string>();
                    }
                    else
                    {
                        VendorData.RegionID = (from reg in _context.tblRegions where reg.Active == true && reg.CountryID == 1 orderby reg.RegionDesc select new { Key = reg.RegionID, Value = reg.RegionDesc }).ToDictionary(o => o.Key, o => o.Value);
                        if (VendorData.RegionID == null)
                        {
                            VendorData.RegionID = new Dictionary<int, string>();
                            VendorData.AreaID = new Dictionary<int, string>();
                        }
                        else if (VendorData.RegionID.Any(reg => reg.Key == VendorData.SelectedRegionID))
                        {
                            VendorData.AreaID = (from area in _context.tblAreas where area.RegionID == VendorData.SelectedRegionID && area.Active == true orderby area.AreaDesc select new { Key = area.AreaID, Value = area.AreaDesc }).ToDictionary(o => o.Key, o => o.Value);
                        }
                        else
                            VendorData.AreaID = new Dictionary<int, string>();
                        VendorData.City = (from city in _context.tblCities where city.StateID == VendorData.SelectedStateID && city.Active == true orderby city.City select new { Key = city.CityID, Value = city.City }).ToDictionary(o => o.Key, o => o.Value);
                        if (VendorData.City == null)
                            VendorData.City = new Dictionary<int, string>();
                        if (VendorData.CityID == null && VendorData.SelectedCity != null && VendorData.SelectedCity != "")
                        {
                            VendorData.City.Add(-1, VendorData.SelectedCity);
                            VendorData.CityID = -1;
                        }
                    }
                }
                else
                {
                    VendorData.StateID = new Dictionary<string, string>();
                    if (VendorData.SelectedCountryID == null)
                    {
                        VendorData.City = new Dictionary<int, string>();
                        VendorData.RegionID = new Dictionary<int, string>();
                        VendorData.AreaID = new Dictionary<int, string>();
                    }
                    else
                    {
                        if (VendorData.RegionID == null)
                        {
                            VendorData.RegionID = new Dictionary<int, string>();
                            VendorData.AreaID = new Dictionary<int, string>();
                        }
                        else if (VendorData.RegionID.Any(reg => reg.Key == VendorData.SelectedRegionID))
                        {
                            VendorData.AreaID = (from area in _context.tblAreas where area.RegionID == VendorData.SelectedRegionID && area.Active == true orderby area.AreaDesc select new { Key = area.AreaID, Value = area.AreaDesc }).ToDictionary(o => o.Key, o => o.Value);
                        }
                        else
                            VendorData.AreaID = new Dictionary<int, string>();
                        VendorData.City = (from city in _context.tblCities where city.CountryID == VendorData.SelectedCountryID && city.Active == true orderby city.City select new { Key = city.CityID, Value = city.City }).ToDictionary(o => o.Key, o => o.Value);
                        if (VendorData.City == null)
                            VendorData.City = new Dictionary<int, string>();
                        if (VendorData.CityID == null && VendorData.SelectedCity != null && VendorData.SelectedCity != "")
                        {
                            VendorData.City.Add(-1, VendorData.SelectedCity);
                            VendorData.CityID = -1;
                        }
                    }
                }
            }
            VendorData.CountryID = (from cnt in _context.tblCountries where cnt.Active == true orderby cnt.CountryName select new { Key = cnt.CountryID, Value = cnt.CountryName }).ToDictionary(o => o.Key, o => o.Value);
            VendorData.VendorTypeID = (from vtypes in _context.tblVendorTypes where vtypes.Active == true select new { Key = vtypes.VendorTypeID, Value = vtypes.VendorTypeCode }).ToDictionary(o => o.Key, o => o.Value);
            return VendorData;
        }
        public PreferredVendorContractsBData LoadVendorContractsByID(int VendorID)
        {
            PreferredVendorContractsBData VendorContractsData = new PreferredVendorContractsBData();
            VendorContractsData.Rows = new List<PreferredVendorContractsRowBData>();
            VendorContractsData.Rows = (from contract in _context.tblContracts
                                        from season in _context.tblSeasons
                                        where contract.VendorID == VendorID
                                        && contract.SeasonID == season.SeasonID
                                        select new PreferredVendorContractsRowBData
                                        {
                                            VendorID = contract.VendorID,
                                            SelectedSeasonID = contract.SeasonID,
                                            ContractDate = contract.ContractDate,
                                            ContractStartDate = contract.ContractStartDate,
                                            ContractEndDate = contract.ContractEndDate,
                                            SeasonCode = season.SeasonCode,
                                            SeasonDescription = season.SeasonDescription,
                                        }).ToList();
            VendorContractsData.SeasonCode = (from season in _context.tblSeasons
                                              select new
                                              {
                                                  Key = season.SeasonID,
                                                  Value = season.SeasonCode
                                              }).ToDictionary(o => o.Key, o => o.Value);
            VendorContractsData.SeasonDescription = (from sdesc in _context.tblSeasons
                                                     select new
                                                     {
                                                         Key = sdesc.SeasonID,
                                                         Value = sdesc.SeasonDescription
                                                     }).ToDictionary(o => o.Key, o => o.Value);
            return VendorContractsData;
        }
        public PreferredVendorContractsFromDetailsBData LoadVendorContractDetailsByID(int VendorID, int SeasonID)
        {
            PreferredVendorContractsFromDetailsBData VendorContractDetailsData = new PreferredVendorContractsFromDetailsBData();
            VendorContractDetailsData.Rows = new List<PreferredVendorContractsDetailBData>();
            VendorContractDetailsData.Rows = (from contractDetails in _context.tblContractDetails
                                              from Services in _context.tblServices
                                              from ServiceTypes in _context.tblServiceTypes
                                              where contractDetails.VendorID == VendorID
                                              && contractDetails.SeasonID == SeasonID
                                              && Services.ServiceID == contractDetails.ServiceID
                                              && ServiceTypes.ServiceTypeID == Services.ServiceTypeID
                                              && Services.Active == true
                                              && ServiceTypes.Active == true
                                              select new PreferredVendorContractsDetailBData()
                                              {
                                                  LineNo = contractDetails.LineNo,
                                                  ServiceID = contractDetails.ServiceID,
                                                  Description = contractDetails.Description,
                                                  ServiceCode = Services.ServiceCode,
                                                  ServiceTypeCode = ServiceTypes.ServiceTypeCode,
                                                  CurrencyText = contractDetails.Currency,
                                                  Price = contractDetails.Price,
                                                  StartDate = contractDetails.StartDate,
                                                  EndDate = contractDetails.EndDate,
                                              }).ToList();
            VendorContractDetailsData.ContractTableDetail = (from contract in _context.tblContracts
                                                             from Vendor in _context.tblVendors
                                                             from VendorType in _context.tblVendorTypes
                                                             from Season in _context.tblSeasons
                                                             where VendorID == contract.VendorID
                                                             && SeasonID == contract.SeasonID
                                                             && VendorID == Vendor.VendorID
                                                             && Vendor.VendorTypeID == VendorType.VendorTypeID
                                                             && SeasonID == Season.SeasonID
                                                             && VendorType.Active == true
                                                             select new PreferredVendorContractForDetail()
                                                             {
                                                                 Luggage = contract.Luggage,
                                                                 Tax = contract.Tax,
                                                                 OccTax = contract.OccTax,
                                                                 SelectedCurrency = contract.Currency,
                                                                 Note = contract.Note,
                                                                 ContractDate = contract.ContractDate,
                                                                 ContractStartDate = contract.ContractStartDate,
                                                                 ContractEndDate = contract.ContractEndDate,
                                                                 Gratuity = contract.Gratuity,
                                                                 VendorID = contract.VendorID,
                                                                 SelectedSeasonID = contract.SeasonID,
                                                                 VendorName = Vendor.VendorName,
                                                                 VendorTypeCode = VendorType.VendorTypeCode,
                                                                 SeasonCode = Season.SeasonCode,
                                                                 SeasonDescription = Season.SeasonDescription,
                                                                 ContractFile = contract.ContractFile,
                                                             }).SingleOrDefault();
            VendorContractDetailsData.Services = (from vnd in _context.tblVendors
                                                  from Service in _context.tblServices
                                                  from ServiceTypes in _context.tblServiceTypes
                                                  where vnd.VendorID == VendorID
                                                  && vnd.VendorTypeID == ServiceTypes.VendorTypeID
                                                  && Service.ServiceTypeID == ServiceTypes.ServiceTypeID
                                                  && Service.Active == true
                                                  && ServiceTypes.Active == true
                                                  && vnd.Active == true
                                                  orderby ServiceTypes.ServiceTypeCode
                                                  select new
                                                  {
                                                      Key = Service.ServiceID,
                                                      Value = ServiceTypes.ServiceTypeCode + " - " + Service.ServiceCode
                                                  }).ToDictionary(o => o.Key, o => o.Value);
            VendorContractDetailsData.Currency = new Dictionary<int, string>
            {
                {1,"USD"},
                {2,"CAD"},
            };
            return VendorContractDetailsData;
        }
        public PreferredVendorGroupsBData LoadVendorGroupsByID(int VendorID)
        {
            PreferredVendorGroupsBData VendorGroupsData = new PreferredVendorGroupsBData();
            VendorGroupsData.Rows = new List<PreferredVendorGroupsRowBData>();
            VendorGroupsData.Rows = (from VendorConfirm in _context.tblVendorConfirms
                                     from GroupStatus in _context.tblGroupStatus
                                     from Group in _context.tblGroups
                                     from ConfirmStatus in _context.tblVConfirmStatus
                                     from Customers in _context.tblCustomers
                                     where VendorConfirm.VendorID == VendorID
                                     && VendorConfirm.GroupID == Group.GroupID
                                     && VendorConfirm.StatusID == ConfirmStatus.StatusID
                                     && VendorConfirm.StatusID == GroupStatus.StatusID
                                     && Group.CustomerID == Customers.CustomerID
                                     select new PreferredVendorGroupsRowBData
                                        {
                                            VendorID = VendorConfirm.VendorID,
                                            GroupID = VendorConfirm.GroupID,
                                            ArrivalDate = Group.ArrivalDate,
                                            DepartureDate = Group.DepartureDate,
                                            CoustomerCode = Customers.CustomerCode,
                                            GroupCode = Group.GroupCode,
                                            GroupName = Group.GroupName,
                                            GroupStatusDesc = GroupStatus.StatusDesc,
                                            ConfirmStatusDesc = ConfirmStatus.StatusDesc,
                                            Pax = VendorConfirm.Pax,
                                            Rooms = Group.Rooms,
                                        }).ToList();
            return VendorGroupsData;
        }
        public ErrorBInfo SaveVendorDetailsData(PreferredVendorDetailsBData Data)
        {
            ErrorBInfo Error = new ErrorBInfo();
            try
            {
                var EditVendor = (from Vendor in _context.tblVendors where Vendor.VendorID == Data.VendorID select Vendor).SingleOrDefault();
                if (EditVendor != null)
                {
                    EditVendor.VendorTypeID = Data.SelectedVendorTypeID;
                    EditVendor.VendorName = Data.VendorName;
                    EditVendor.VendorCode = Data.VendorName.Length > 20 ? Data.VendorName.Substring(0, 19) : Data.VendorName;
                    EditVendor.ContactName = Data.ContactName;
                    EditVendor.ContactTitle = Data.ContactTitle;
                    EditVendor.Preferred = Data.Preferred;
                    EditVendor.Active = Data.Active;
                    EditVendor.IsFind = Data.IsFind;
                    EditVendor.IsComp = Data.IsComp;
                    EditVendor.Address = Data.Address;
                    EditVendor.City = Data.CityID == -1 ? Data.SelectedCity : (from city in _context.tblCities where city.CityID == Data.CityID select city.City).SingleOrDefault();
                    EditVendor.CityID = Data.CityID != -1 ? Data.CityID : null;
                    EditVendor.StateID = Data.SelectedStateID;
                    EditVendor.PostalCode = Data.PostalCode;
                    EditVendor.Phone800 = Data.Phone800;
                    EditVendor.PhoneNumber = Data.PhoneNumber;
                    EditVendor.PhoneSales = Data.PhoneSales;
                    EditVendor.FaxNumber = Data.FaxNumber;
                    EditVendor.FaxSales = Data.FaxSales;
                    EditVendor.RegionID = Data.SelectedRegionID;
                    EditVendor.AreaID = Data.SelectedAreaID;
                    EditVendor.CountryID = Data.SelectedCountryID;
                    EditVendor.Notes = Data.Notes;
                    EditVendor.ArrivalDate = Data.ArrivalDate;
                    EditVendor.DepartureDate = Data.DepartureDate;
                    EditVendor.EmailName = Data.Email;
                    EditVendor.EmailNameSales = Data.Email;
                    _context.SaveChanges();
                }
                else
                {
                    Error.errorCode = 130;
                    Error.Message = "Vendor Can`t be found:";
                }
            }
            catch (Exception ex)
            {
                Error.errorCode = 130;
                Error.Message = ex.Message;
                logger.Error(ex.ToMessageAndCompleteStacktrace());
            }
            return Error;
        }
        public ErrorBInfo SaveVendorContractsData(List<PreferredVendorContractsRowBData> Data, int VendorID)
        {
            ErrorBInfo Error = new ErrorBInfo();
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    List<int> SeasonList = Data.Select(Ctr => Ctr.OldSelectedSeasonID).ToList();
                    var DelEntityContracts = _context.tblContracts.Where(ctr => ctr.VendorID == VendorID && SeasonList.Contains(ctr.SeasonID)).ToList();
                    var DelEntityContractsDetails = _context.tblContractDetails.Where(ctrdet => ctrdet.VendorID == VendorID && SeasonList.Contains(ctrdet.SeasonID)).ToList();
                    _context.tblContractDetails.RemoveRange(DelEntityContractsDetails);
                    _context.SaveChanges();
                    _context.tblContracts.RemoveRange(DelEntityContracts);
                    _context.SaveChanges();
                    // adding new
                    foreach (var Contr in DelEntityContracts)
                    {
                        var NewData = Data.Where(data => data.OldSelectedSeasonID == Contr.SeasonID).SingleOrDefault();
                        if (NewData != null)
                        {
                            Contr.SeasonID = NewData.SelectedSeasonID;
                            Contr.ContractDate = NewData.ContractDate;
                            Contr.ContractStartDate = NewData.ContractStartDate;
                            Contr.ContractEndDate = NewData.ContractEndDate;
                        }
                    }
                    foreach (var Contr in DelEntityContractsDetails)
                    {
                        var NewDataDetails = Data.Where(data => data.OldSelectedSeasonID == Contr.SeasonID).ToList();

                        if (NewDataDetails != null)
                        {
                            foreach (var NewContr in NewDataDetails)
                            {
                                Contr.SeasonID = NewContr.SelectedSeasonID;
                            }
                        }
                    }
                    _context.tblContracts.AddRange(DelEntityContracts);
                    _context.tblContractDetails.AddRange(DelEntityContractsDetails);
                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    Error.errorCode = 100;
                    Error.Message = ex.Message;
                    logger.Error(ex.ToMessageAndCompleteStacktrace());
                    transaction.Rollback();
                }
            }
            return Error;
        }
        public ErrorBInfo SaveVendorContractDetailsData(PreferredVendorContractsFromDetailsBData VendorContractDetailsData)
        {
            ErrorBInfo Error = new ErrorBInfo();
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    List<int> LineNoList = VendorContractDetailsData.Rows.Select(VenContractDet => VenContractDet.LineNo).ToList();
                    var SaveEntityContractDetails = _context.tblContractDetails.Where(ctrdet => ctrdet.VendorID == VendorContractDetailsData.ContractTableDetail.VendorID
                                                                                             && ctrdet.SeasonID == VendorContractDetailsData.ContractTableDetail.SelectedSeasonID
                                                                                             && LineNoList.Contains(ctrdet.LineNo)).ToList();
                    foreach (var ContrDet in SaveEntityContractDetails)
                    {
                        var SaveData = VendorContractDetailsData.Rows.Where(data => data.LineNo == ContrDet.LineNo).SingleOrDefault();
                        if (SaveData != null)
                        {
                            ContrDet.Price = SaveData.Price;
                            ContrDet.StartDate = SaveData.StartDate;
                            ContrDet.EndDate = SaveData.EndDate;
                            ContrDet.Description = SaveData.Description;
                            ContrDet.ServiceID = SaveData.ServiceID;
                            ContrDet.Currency = SaveData.CurrencyText;
                        }
                    }
                    var EditContractsDetails = (from Contract in _context.tblContracts
                                                where VendorContractDetailsData.ContractTableDetail.VendorID == Contract.VendorID
                                                && VendorContractDetailsData.ContractTableDetail.SelectedSeasonID == Contract.SeasonID
                                                select Contract).SingleOrDefault();
                    EditContractsDetails.ContractDate = VendorContractDetailsData.ContractTableDetail.ContractDate;
                    EditContractsDetails.ContractStartDate = VendorContractDetailsData.ContractTableDetail.ContractStartDate;
                    EditContractsDetails.ContractEndDate = VendorContractDetailsData.ContractTableDetail.ContractEndDate;
                    EditContractsDetails.Gratuity = VendorContractDetailsData.ContractTableDetail.Gratuity;
                    EditContractsDetails.Luggage = VendorContractDetailsData.ContractTableDetail.Luggage;
                    EditContractsDetails.Note = VendorContractDetailsData.ContractTableDetail.Note;
                    EditContractsDetails.OccTax = VendorContractDetailsData.ContractTableDetail.OccTax;
                    EditContractsDetails.Tax = VendorContractDetailsData.ContractTableDetail.Tax;
                    EditContractsDetails.Currency = VendorContractDetailsData.ContractTableDetail.SelectedCurrency;

                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    Error.errorCode = 100;
                    Error.Message = ex.Message;
                    logger.Error(ex.ToMessageAndCompleteStacktrace());
                    transaction.Rollback();
                }
            }
            return Error;
        }
        public ErrorBInfo UpdateVendorContractPath(int VendorID, int SeasonID, string VendorContractPath)
        {
            ErrorBInfo Error = new ErrorBInfo();
            var SaveEntityContract = _context.tblContracts.Where(ctrdet => ctrdet.VendorID == VendorID && ctrdet.SeasonID == SeasonID).FirstOrDefault();
            if (SaveEntityContract != null)
            {
                try
                {
                    SaveEntityContract.ContractFile = VendorContractPath;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Error.errorCode = 100;
                    Error.Message = ex.Message;
                    logger.Error(ex.ToMessageAndCompleteStacktrace());
                }
            }
            return Error;
        }
        public Dictionary<int, string> LoadArea(int RegionID)
        {
            Dictionary<int, string> Area = new Dictionary<int, string>();
            if (RegionID != 0)
            {
                Area = (from area in _context.tblAreas where area.RegionID == RegionID && area.Active == true orderby area.AreaDesc select new { Key = area.AreaID, Value = area.AreaDesc }).ToDictionary(o => o.Key, o => o.Value);
            }
            if (Area == null)
                Area = new Dictionary<int, string>();
            return Area;
        }
        public Dictionary<int, string> LoadRegion(int CountryID)
        {
            Dictionary<int, string> Region = new Dictionary<int, string>();
            if (CountryID != 0)
            {
                Region = (from reg in _context.tblRegions where reg.CountryID == CountryID && reg.Active == true orderby reg.RegionDesc select new { Key = reg.RegionID, Value = reg.RegionDesc }).ToDictionary(o => o.Key, o => o.Value);
            }
            if (Region == null)
                Region = new Dictionary<int, string>();
            return Region;
        }
        public Dictionary<string, string> LoadState()
        {
            Dictionary<string, string> States = new Dictionary<string, string>();
            States = (from state in _context.tblStates where state.Active == true select new { Key = state.StateID, Value = state.StateID }).ToDictionary(o => o.Key, o => o.Value);
            if (States == null)
            {
                States = new Dictionary<string, string>();
            }
            return States;
        }
        public Dictionary<int, string> LoadCity(int CountryID, string StateID)
        {
            Dictionary<int, string> City = new Dictionary<int, string>();
            if (CountryID != 0)
            {
                City = (from city in _context.tblCities where city.CountryID == CountryID && city.Active == true orderby city.City select new { Key = city.CityID, Value = city.City }).ToDictionary(o => o.Key, o => o.Value);
            }
            else
            {
                City = (from city in _context.tblCities where city.StateID == StateID && city.Active == true orderby city.City select new { Key = city.CityID, Value = city.City }).ToDictionary(o => o.Key, o => o.Value);
            }
            if (City == null)
                City = new Dictionary<int, string>();
            return City;
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
